<?php

namespace App\Admin\Controllers\Service;

use App\Http\Controllers\Controller;
use App\Models\Service;
use App\Models\ServiceType;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use function foo\func;

class ServiceTypeController extends AdminController
{

    public $title = '业务类型';


    /**
     * 列表内容
     * @return Grid
     */
    public function grid ()
    {
        $grid = new Grid(new ServiceType);
        $grid->model()->with('service');
        $grid->id;
        $grid->column('name', '类型名称');
        $grid->column('service.name', '所属类别');
        $grid->column('status', '状态')->display(function ($status) {
            return Service::STATUS[$status];
        });
        $grid->updated_at;
        $grid->created_at;

        $grid->filter(function (Grid\Filter $filter){
            $filter->equal('service_id', '所属类别')->select(Service::query()->where('status', 1)->orderBy('id')->pluck('name', 'id'));
            $filter->like('name', '类型名称');
        });

        $grid->disableDeleteButton();
        return $grid;
    }


    /**
     * 表单内容
     * @return Form
     */
    public function form()
    {
        $form = new Form(new ServiceType());
        $form->display('id');
        $form->select('service_id', '业务类别')
            ->options(Service::query()->where('status', 1)->orderBy('id')->pluck('name', 'id'));
        $form->text('name', '类型名称')->rules(function ($form){
            $rules = 'required|max:20|unique:service_type,name';
            $id = $form->model()->id;
            $rules .= ",{$id},id";
            $rules .= ',service_id,'. $form->service_id;
            $rules .= ',deleted_at,NULL';

            return $rules;
        });
        $form->image('img', '图片')->move('service/type', uniqid());
        $form->switch('status', '状态')->default(1);
        $form->disableDeleteButton();
        return $form;
    }


    /**
     * 详情内容
     * @param $id
     */
    public function detail($id)
    {
        $show = new Show($id, new ServiceType());
        $show->id;
        $show->field('name', '类型名称');
        $show->field('img', '图片')->image();
        $show->field('service_id', '所属类别')->as(function ($service_id) {
            return Service::query()->find($service_id)->value('name');
        });

        $show->disableDeleteButton();
        $show->updated_at;
        $show->created_at;

        return $show;
    }
}
